package inc.zhugastrov.sellerform;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ComplOrderPane extends JPanel{
	private static ComplOrderPane instance = new ComplOrderPane();
	private JTable catalog;
	private JTable items;
	private OrdersTableModel model = new OrdersTableModel();
	private ItemsTableModel itemsModel = new ItemsTableModel();
	private JScrollPane scrollPane;
	private JScrollPane scrollItemsPane;

	private ComplOrderPane() {
		init();
	}

	public static ComplOrderPane getInstance() {
		return instance;
	}

	public void addOrder(OrderIntr order) {
		model.addRow(order);
	}

	private void init() {

		catalog = new JTable();
		catalog.setModel(model);
		catalog.getTableHeader().setReorderingAllowed(false);
		catalog.setFocusable(false);
		catalog.setSelectionMode(0);
		catalog.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				itemsModel.emptyTable();
				List<OrderedItems> products = model.getRow(
						catalog.getSelectedRow()).getItems();
				for (Product item : products) {
					itemsModel.addRow(item);
					items.updateUI();
				}
			}

		});
		scrollPane = new JScrollPane(catalog);
		catalog.setPreferredScrollableViewportSize(new java.awt.Dimension(500,
				300));
		add(scrollPane);

		items = new JTable();
		items.setModel(itemsModel);
		items.getTableHeader().setReorderingAllowed(false);
		items.setFocusable(false);
		items.setSelectionMode(0);
		scrollItemsPane = new JScrollPane(items);
		items.setPreferredScrollableViewportSize(new java.awt.Dimension(500,
				150));
		add(scrollItemsPane);

	}

	
	

}
